package com.gct.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import com.gct.interceptor.ScriptInterceptor;

@Configuration
@EnableWebMvc
@Import(value={ServiceConfig.class})
@ComponentScan(basePackages = {"com.gct.controller"})
public class GctMvcConfig extends WebMvcConfigurerAdapter {
    private static final int CACHE_PERIOD = 0; // one year

    @Autowired
    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter;

    @Bean(name="mappingJackson2HttpMessageConverter")
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
        return new MappingJackson2HttpMessageConverter();
    }

    @Bean(name="requestMappingHandlerAdapter")
    public RequestMappingHandlerAdapter requestMappingHandlerAdapter(){
        RequestMappingHandlerAdapter rsh = new RequestMappingHandlerAdapter();
        rsh.getMessageConverters().add(mappingJackson2HttpMessageConverter);
        return rsh;
    }

    @Bean(name="viewResolver")
    public ViewResolver viewResolver(){
        InternalResourceViewResolver intRVR = new InternalResourceViewResolver();
        intRVR.setViewClass(JstlView.class);
        intRVR.setPrefix("/WEB-INF/view/");
        intRVR.setSuffix(".jsp");
        return intRVR;
    }

    @Bean
    public CommonsMultipartResolver multipartResolver() {
        CommonsMultipartResolver resolver = new CommonsMultipartResolver();
        resolver.setMaxUploadSize(1073741824); // 1 gigabyte
        resolver.setDefaultEncoding("utf-8");
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
            .addResourceHandler("/html/**")
                .addResourceLocations("/html/")
                .setCachePeriod(CACHE_PERIOD);
        registry
            .addResourceHandler("/script/**")
                .addResourceLocations("/script/")
                .setCachePeriod(CACHE_PERIOD);
        registry
        .addResourceHandler("/style/**")
            .addResourceLocations("/style/")
            .setCachePeriod(CACHE_PERIOD);

        registry
        .addResourceHandler("/img/**")
            .addResourceLocations("/img/")
            .setCachePeriod(CACHE_PERIOD);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new ScriptInterceptor()).addPathPatterns("/upload/save");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        // Serving static files using the Servlet container's default Servlet.
        configurer.enable();
    }
}
